package com.demo.hjh.thread;

public class Method2 {  
    private int num = 10;  
      
    private Object lock = new Object();  
      
  
    public synchronized void method() {  
        // 当前线程持有对象lock的锁 那么下面应用条件对象时就必须调用锁的相应的方法  
        synchronized(lock) {  
            if (num > 8) {  
                for (int i = 0; i<3 ;i++) {  
                    System.out.println(Thread.currentThread().getName() + " : " + num);  
                    num--;  
                }  
                try {  
                    System.out.println(Thread.currentThread().getName() + " wait begin ");  
                    //调用父类Object的native方法wait()  
//              super.wait();  
//              this.wait();  
//              wait();  
                    // 调用显示锁lock的wait()方法  
                    lock.wait();  
                    System.out.println(Thread.currentThread().getName() + " wait end ");  
                } catch (InterruptedException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }   
              
            if (num <=7 && num > 1) {  
                for (int i = 0; i<7 ;i++) {  
                    System.out.println(Thread.currentThread().getName() + " : " + num);  
                    num--;  
                }  
                //调用父类Object的native方法wait()  
//          super.notify();  
//          this.notify();        
//          notify();  
                // 调用显示锁lock的notify()方法  
                lock.notify();  
                System.out.println(Thread.currentThread().getName() + " notify ");  
            }     
        }  
      
    }  
      
    public static void main(String [] args) {  
        final Method2 m = new Method2();  
        Thread t1= new Thread(new Runnable() {  
            public void run() {  
                m.method();  
            }  
        },"A");  
          
        Thread t2= new Thread(new Runnable() {  
            public void run() {  
                m.method();  
            }  
        },"B");  
        t1.start();  
        t2.start();  
    }  
}
